import Vue from "vue";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import App from "./App.vue";
import router from "./router";
// import store from "./store";

import Cookies from "js-cookie";

import store from "./store/index";

import tim from "tim";
import TIM from "tim-js-sdk";
import "./assets/icon/iconfont.css";
import "./assets/icon/iconfont1.css";
import "./assets/css/base.css";
import "./assets/css/icon.css";

import "./assets/css/index.less";
import "font-awesome.css";
import api from "./assets/js/request/api/index";
Vue.prototype.$api = api;
import VueQuillEditor from "vue-quill-editor";

import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
Vue.use(VueQuillEditor);

import Avatar from "@/components/currency/avatar.vue";
import tableComponents from "@/components/table-component/table-component";
import "@babel/polyfill";
import Es6Promise from "es6-promise";
Es6Promise.polyfill();

import { Message, MessageBox } from "element-ui";

window.tim = tim;

window.TIM = TIM;

// window.store = store;
window.$message = Message;
Vue.prototype.$bus = new Vue(); // event Bus 用于无关系组件间的通信。

Vue.prototype.tim = tim;
Vue.prototype.TIM = TIM;

// Vue.prototype.$store = store;

Vue.prototype.$message = Message;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.component("avatar", Avatar);
Vue.component("tableComponents", tableComponents); //表格页面模板

//引入vant 图标库
import { Icon } from "vant";
import "vant/lib/icon/style";
Vue.use(Icon);

// 引入 echarts
import echarts from "echarts";
Vue.prototype.$echarts = echarts;

import getOptions from "./assets/js/getOptions";
Vue.prototype.$getOptions = getOptions;
//弹框插件
import VueSweetalert2 from "vue-sweetalert2";
const options = {
  confirmButtonColor: "#41b882",
  cancelButtonColor: "#ff7674",
};
Vue.use(VueSweetalert2, options);
//注册过滤器
import filters from "./assets/js/filter/filter";
Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key]);
});
//引入自定义方法
import fnc from "./assets/js/fnc/fnc";
Vue.prototype.$fnc = fnc;
// 引入页签
import generateKeyPair from "./assets/js/createRsa";
import getSign from "./assets/js/createSign";
import getUpData from "./assets/js/getUpData";

Vue.prototype.$getCode = generateKeyPair;
Vue.prototype.$getSign = getSign;
Vue.prototype.$getUpData = getUpData;

Vue.config.productionTip = true;

import VueCropper from "vue-cropper";
Vue.use(VueCropper);

Vue.directive("Int", {
  bind: function(el) {
    const input = el.getElementsByTagName("input")[0];
    input.onkeyup = function() {
      if (input.value.length === 1) {
        input.value = input.value.replace(/[^\d\.]/g, "");
      } else {
        // eslint-disable-next-line no-useless-escape
        input.value = input.value.replace(/[^\d\.]/g, "");
      }
      trigger(input, "input");
    };
    input.onblur = function() {
      if (input.value.length === 1) {
        input.value = input.value.replace(/[^\d\.]/g, "");
      } else {
        // eslint-disable-next-line no-useless-escape
        input.value = input.value.replace(/[^\d\.]/g, "");
      }
      if (input.value.indexOf(".00") >= 0) {
        input.value = fnc.toFixedZ(input.value, 0);
      } else if (input.value.indexOf(".") >= 0) {
        input.value = fnc.toFixedZ(input.value, 2);
      }
      trigger(input, "input");
    };
    setTimeout(() => {
      if (input.value.indexOf(".00") >= 0) {
        input.value = fnc.toFixedZ(input.value, 0);
      } else if (input.value.indexOf(".") >= 0) {
        input.value = fnc.toFixedZ(input.value, 2);
      }
    }, 100);
    input.oninput = function() {
      console.log(input.value);
    };
  },
});

/***
 * 防抖 单位时间只触发最后一次
 *  @param {?Number|600} time - 间隔时间
 *  @param {Function} fn - 执行事件
 *  @param {?String|"click"} event - 事件类型 例："click"
 *  @param {Array} binding.value - [fn,event,time]
 *  例：<el-button v-debounce="[reset,`click`,300]">刷新</el-button>
 *  也可简写成：<el-button v-debounce="[reset]">刷新</el-button>
 */
Vue.directive("debounce", {
  inserted: function(el, binding) {
    let [fn, event = "click", time = 600] = binding.value;
    let timer;
    el.addEventListener(event, () => {
      timer && clearTimeout(timer);
      timer = setTimeout(() => fn(), time);
    });
  },
});
/***
 *  节流 每单位时间可触发一次
 *  第一次瞬间触发，最后一次不管是否达到间隔时间依然触发
 * 【考虑到input的change事件】
 *  @param {?Number|600} time - 间隔时间
 *  @param {Function} fn - 执行事件
 *  @param {?String|"click"} event - 事件类型 例："click"
 *  @param {Array} binding.value - [fn,event,time]
 *  例：<el-button v-throttle="[reset,`click`,300]">刷新</el-button>
 *  传递参数则：<el-button v-throttle="[()=>reset(param),`click`,300]">刷新</el-button>
 */
Vue.directive("throttle", {
  inserted: function(el, binding) {
    let [fn, event = "click", time = 600] = binding.value;
    let timer, timer_end;
    el.addEventListener(event, () => {
      if (timer) {
        clearTimeout(timer_end);
        return (timer_end = setTimeout(() => fn(), time));
      }
      fn();
      timer = setTimeout(() => (timer = null), time);
    });
  },
});

const trigger = (el, type) => {
  const e = document.createEvent("HTMLEvents");
  e.initEvent(type, true, true);
  el.dispatchEvent(e);
};

router.beforeEach((to, from, next) => {
  var adminUid = Cookies.get("adminuid") || false;
  if (to.path.indexOf("plugin-index") >= 0) {
    next();
  } else if (adminUid) {
    // 判断当前用户的登录信息loginInfo是否存在
    if (
      (to.fullPath == "/" || to.fullPath == "/index") &&
      localStorage.getItem("first_menu") &&
      localStorage.getItem("first_menu") != "/index" &&
      adminUid
    ) {
      next({
        path: localStorage.getItem("first_menu"),
      });
    } else {
      next();
    }
  } else if (to.path.indexOf("login") == -1) {
    next({
      path: "/login",
      query: {
        redirect: to.fullPath,
      },
    });
  } else {
    next();
  }
});

Vue.directive("enterNumber", {
  inserted: function(el) {
    el.addEventListener("keypress", function(e) {
      e = e || window.event;
      let charcode = typeof e.charCode == "number" ? e.charCode : e.keyCode;
      let re = /\d/;
      if (
        !re.test(String.fromCharCode(charcode)) &&
        charcode > 9 &&
        !e.ctrlKey
      ) {
        if (e.preventDefault) {
          e.preventDefault();
        } else {
          e.returnValue = false;
        }
      }
    });
  },
});

import x2js from "x2js"; //xml数据处理插件 xml转JSON
Vue.prototype.$x2js = new x2js(); //创建x2js对象，挂到vue原型上
Vue.use(ElementUI);
new Vue({
  router,
  store,
  render: (h) => h(App),
  mounted() {
    document.dispatchEvent(new Event("render-event"));
  },
}).$mount("#app");
